2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_IMAGEBUTTONHANDLER_JUCEHEADER__
27 #define __JUCER_IMAGEBUTTONHANDLER_JUCEHEADER__
29 #include "jucer_ButtonHandler.h"
30 #include "../../properties/jucer_ComponentColourProperty.h"
31 #include "../paintelements/jucer_ImageResourceProperty.h"
34 //==============================================================================
37 class ImageButtonHandler
: public ButtonHandler
47 //==============================================================================
49 : ButtonHandler ("Image Button", "ImageButton", typeid (ImageButton
), 150, 24)
53 //==============================================================================
54 Component
* createNewComponent (JucerDocument
*)
56 return new ImageButton ("new button");
59 void getEditableProperties (Component
* component
, JucerDocument
& document
, Array
<PropertyComponent
*>& properties
)
61 ButtonHandler::getEditableProperties (component
, document
, properties
);
63 addColourProperties (component
, document
, properties
);
65 ImageButton
* const ib
= (ImageButton
*) component
;
67 ComponentLayout
& layout
= *document
.getComponentLayout();
69 properties
.add (new ImageButtonProportionProperty (layout
, ib
));
71 properties
.add (new ImageButtonResourceProperty (layout
, ib
, normalImage
, "normal image"));
72 properties
.add (new ImageButtonOpacityProperty (layout
, ib
, "opacity", normalImage
));
73 properties
.add (new ImageButtonColourProperty (layout
, ib
, "overlay col.", normalImage
));
75 properties
.add (new ImageButtonResourceProperty (layout
, ib
, overImage
, "over image"));
76 properties
.add (new ImageButtonOpacityProperty (layout
, ib
, "opacity", overImage
));
77 properties
.add (new ImageButtonColourProperty (layout
, ib
, "overlay col.", overImage
));
79 properties
.add (new ImageButtonResourceProperty (layout
, ib
, downImage
, "down image"));
80 properties
.add (new ImageButtonOpacityProperty (layout
, ib
, "opacity", downImage
));
81 properties
.add (new ImageButtonColourProperty (layout
, ib
, "overlay col.", downImage
));
84 XmlElement
* createXmlFor (Component
* comp
, const ComponentLayout
* layout
)
86 XmlElement
* e
= ButtonHandler::createXmlFor (comp
, layout
);
88 ImageButton
* const ib
= (ImageButton
*) comp
;
90 e
->setAttribute ("keepProportions", doesImageKeepProportions (ib
));
92 e
->setAttribute ("resourceNormal", getImageResource (ib
, normalImage
));
93 e
->setAttribute ("opacityNormal", getImageOpacity (ib
, normalImage
));
94 e
->setAttribute ("colourNormal", getImageColour (ib
, normalImage
).toString());
96 e
->setAttribute ("resourceOver", getImageResource (ib
, overImage
));
97 e
->setAttribute ("opacityOver", getImageOpacity (ib
, overImage
));
98 e
->setAttribute ("colourOver", getImageColour (ib
, overImage
).toString());
100 e
->setAttribute ("resourceDown", getImageResource (ib
, downImage
));
101 e
->setAttribute ("opacityDown", getImageOpacity (ib
, downImage
));
102 e
->setAttribute ("colourDown", getImageColour (ib
, downImage
).toString());
107 bool restoreFromXml (const XmlElement
& xml
, Component
* comp
, const ComponentLayout
* layout
)
109 if (! ButtonHandler::restoreFromXml (xml
, comp
, layout
))
112 ImageButton
* const ib
= (ImageButton
*) comp
;
113 ComponentLayout
& l
= const_cast <ComponentLayout
&> (*layout
);
115 setImageKeepProportions (l
, ib
, xml
.getBoolAttribute ("keepProportions", true), false);
117 setImageResource (l
, ib
, normalImage
, xml
.getStringAttribute ("resourceNormal", String::empty
), false);
118 setImageOpacity (l
, ib
, normalImage
, (float) xml
.getDoubleAttribute ("opacityNormal", 1.0f
), false);
119 setImageColour (l
, ib
, normalImage
, Colour::fromString (xml
.getStringAttribute ("colourNormal", "0")), false);
121 setImageResource (l
, ib
, overImage
, xml
.getStringAttribute ("resourceOver", String::empty
), false);
122 setImageOpacity (l
, ib
, overImage
, (float) xml
.getDoubleAttribute ("opacityOver", 1.0f
), false);
123 setImageColour (l
, ib
, overImage
, Colour::fromString (xml
.getStringAttribute ("colourOver", "0")), false);
125 setImageResource (l
, ib
, downImage
, xml
.getStringAttribute ("resourceDown", String::empty
), false);
126 setImageOpacity (l
, ib
, downImage
, (float) xml
.getDoubleAttribute ("opacityDown", 1.0f
), false);
127 setImageColour (l
, ib
, downImage
, Colour::fromString (xml
.getStringAttribute ("colourDown", "0")), false);
132 void fillInCreationCode (GeneratedCode
& code
, Component
* component
, const String
& memberVariableName
)
134 ButtonHandler::fillInCreationCode (code
, component
, memberVariableName
);
136 ImageButton
* const ib
= dynamic_cast <ImageButton
*> (component
);
140 s
<< getColourIntialisationCode (component
, memberVariableName
)
143 const String
indent (String::repeatedString (" ", memberVariableName
.length() + 13));
145 s
<< memberVariableName
<< "->setImages (false, true, "
146 << boolToString (doesImageKeepProportions (ib
)) << ",\n"
148 << getImageCreationCode (ib
, normalImage
) << ", "
149 << valueToFloat (getImageOpacity (ib
, normalImage
)) << ", "
150 << colourToCode (getImageColour (ib
, normalImage
)) << ",\n"
152 << getImageCreationCode (ib
, overImage
) << ", "
153 << valueToFloat (getImageOpacity (ib
, overImage
)) << ", "
154 << colourToCode (getImageColour (ib
, overImage
)) << ",\n"
156 << getImageCreationCode (ib
, downImage
) << ", "
157 << valueToFloat (getImageOpacity (ib
, downImage
)) << ", "
158 << colourToCode (getImageColour (ib
, downImage
))
161 code
.constructorCode
+= s
;
164 static const String
getImageCreationCode (ImageButton
* ib
, const ImageRole role
)
166 const String
resName (getImageResource (ib
, role
));
168 if (resName
.isEmpty())
171 return "ImageCache::getFromMemory (" + resName
+ ", " + resName
+ "Size)";
174 //==============================================================================
175 class ImageButtonResourceProperty
: public ImageResourceProperty
<ImageButton
>
178 ImageButtonResourceProperty (ComponentLayout
& layout_
, ImageButton
* const owner_
, const ImageRole role_
, const String
& name
)
179 : ImageResourceProperty
<ImageButton
> (*layout_
.getDocument(), owner_
, name
, true),
185 //==============================================================================
186 void setResource (const String
& newName
)
188 setImageResource (layout
, element
, role
, newName
, true);
191 const String
getResource() const
193 return getImageResource (element
, role
);
197 const ImageRole role
;
198 ComponentLayout
& layout
;
201 class SetImageResourceAction
: public ComponentUndoableAction
<ImageButton
>
204 SetImageResourceAction (ImageButton
* const button
,
205 ComponentLayout
& layout_
,
206 const ImageRole role_
,
207 const String
& newResource_
)
208 : ComponentUndoableAction
<ImageButton
> (button
, layout_
),
209 newResource (newResource_
),
213 oldResource
= ImageButtonHandler::getImageResource (button
, role_
);
219 ImageButtonHandler::setImageResource (layout
, getComponent(), role
, newResource
, false);
226 ImageButtonHandler::setImageResource (layout
, getComponent(), role
, oldResource
, false);
231 String newResource
, oldResource
;
232 const ImageRole role
;
233 ComponentLayout
& layout
;
236 //==============================================================================
237 static void setImageResource (ComponentLayout
& layout
, ImageButton
* button
, const ImageRole role
, const String
& newName
, const bool undoable
)
241 if (role
< 3 && getImageResource (button
, role
) != newName
)
245 layout
.getDocument()->perform (new SetImageResourceAction (button
, layout
, role
, newName
),
246 "Change image resource");
250 button
->getProperties().set ("resource" + String ((int) role
), newName
);
251 updateButtonImages (*layout
.getDocument(), button
);
257 static const String
getImageResource (ImageButton
* button
, const ImageRole role
)
260 return button
->getProperties() ["resource" + String ((int) role
)].toString();
263 //==============================================================================
264 class SetImageKeepsPropAction
: public ComponentUndoableAction
<ImageButton
>
267 SetImageKeepsPropAction (ImageButton
* const button
,
268 ComponentLayout
& layout_
,
269 const bool newState_
)
270 : ComponentUndoableAction
<ImageButton
> (button
, layout_
),
274 oldState
= ImageButtonHandler::doesImageKeepProportions (button
);
280 ImageButtonHandler::setImageKeepProportions (layout
, getComponent(), newState
, false);
287 ImageButtonHandler::setImageKeepProportions (layout
, getComponent(), oldState
, false);
292 bool newState
, oldState
;
293 ComponentLayout
& layout
;
296 static bool doesImageKeepProportions (ImageButton
* button
)
298 return button
->getProperties().getWithDefault ("keepImageProp", true);
301 static void setImageKeepProportions (ComponentLayout
& layout
, ImageButton
* button
, const bool newState
, const bool undoable
)
305 layout
.perform (new SetImageKeepsPropAction (button
, layout
, newState
), "change imagebutton proportion mode");
309 button
->getProperties().set ("keepImageProp", newState
);
310 updateButtonImages (*layout
.getDocument(), button
);
315 class ImageButtonProportionProperty
: public ComponentBooleanProperty
<ImageButton
>
318 ImageButtonProportionProperty (ComponentLayout
& layout_
, ImageButton
* const owner_
)
319 : ComponentBooleanProperty
<ImageButton
> ("proportional", "maintain image proportions", "scale to fit",
320 owner_
, *layout_
.getDocument()),
325 //==============================================================================
326 void setState (bool newState
)
328 setImageKeepProportions (layout
, component
, newState
, true);
331 bool getState() const
333 return doesImageKeepProportions (component
);
337 ComponentLayout
& layout
;
340 //==============================================================================
341 class SetImageOpacityAction
: public ComponentUndoableAction
<ImageButton
>
344 SetImageOpacityAction (ImageButton
* const button
,
345 ComponentLayout
& layout_
,
346 const ImageRole role_
,
347 const float newState_
)
348 : ComponentUndoableAction
<ImageButton
> (button
, layout_
),
353 oldState
= ImageButtonHandler::getImageOpacity (button
, role_
);
359 ImageButtonHandler::setImageOpacity (layout
, getComponent(), role
, newState
, false);
366 ImageButtonHandler::setImageOpacity (layout
, getComponent(), role
, oldState
, false);
371 const ImageRole role
;
372 float newState
, oldState
;
373 ComponentLayout
& layout
;
376 static float getImageOpacity (ImageButton
* button
, const ImageRole role
)
378 return (float) button
->getProperties().getWithDefault ("imageOpacity" + String ((int) role
), 1.0f
);
381 static void setImageOpacity (ComponentLayout
& layout
, ImageButton
* button
, const ImageRole role
, const float opacity
, const bool undoable
)
385 layout
.perform (new SetImageOpacityAction (button
, layout
, role
, opacity
), "change imagebutton opacity");
389 button
->getProperties().set ("imageOpacity" + String ((int) role
), opacity
);
390 updateButtonImages (*layout
.getDocument(), button
);
395 class ImageButtonOpacityProperty
: public SliderPropertyComponent
398 ImageButtonOpacityProperty (ComponentLayout
& layout_
, ImageButton
* const owner_
,
399 const String
& name
, const ImageRole role_
)
400 : SliderPropertyComponent (name
, 0.0, 1.0, 0.0),
407 //==============================================================================
408 void setValue (double newValue
)
410 setImageOpacity (layout
, owner
, role
, (float) newValue
, true);
413 double getValue() const
415 return getImageOpacity (owner
, role
);
419 ImageButton
* const owner
;
420 ComponentLayout
& layout
;
421 const ImageRole role
;
424 //==============================================================================
425 class SetImageColourAction
: public ComponentUndoableAction
<ImageButton
>
428 SetImageColourAction (ImageButton
* const button
,
429 ComponentLayout
& layout_
,
430 const ImageRole role_
,
431 const Colour
& newState_
)
432 : ComponentUndoableAction
<ImageButton
> (button
, layout_
),
437 oldState
= ImageButtonHandler::getImageColour (button
, role_
);
443 ImageButtonHandler::setImageColour (layout
, getComponent(), role
, newState
, false);
450 ImageButtonHandler::setImageColour (layout
, getComponent(), role
, oldState
, false);
455 const ImageRole role
;
456 Colour newState
, oldState
;
457 ComponentLayout
& layout
;
460 static const Colour
getImageColour (ImageButton
* button
, const ImageRole role
)
462 return Colour::fromString (button
->getProperties().getWithDefault ("imageColour" + String ((int) role
), "0").toString());
465 static void setImageColour (ComponentLayout
& layout
, ImageButton
* button
, const ImageRole role
, const Colour
& colour
, const bool undoable
)
469 layout
.perform (new SetImageColourAction (button
, layout
, role
, colour
), "change imagebutton colour");
473 button
->getProperties().set ("imageColour" + String ((int) role
), colour
.toString());
474 updateButtonImages (*layout
.getDocument(), button
);
479 class ImageButtonColourProperty
: public ColourPropertyComponent
,
480 public ChangeListener
483 ImageButtonColourProperty (ComponentLayout
& layout_
, ImageButton
* const owner_
,
484 const String
& name
, const ImageRole role_
)
485 : ColourPropertyComponent (name
, false),
490 layout_
.getDocument()->addChangeListener (this);
493 ~ImageButtonColourProperty()
495 layout
.getDocument()->removeChangeListener (this);
498 //==============================================================================
499 void setColour (const Colour
& newColour
)
501 setImageColour (layout
, owner
, role
, newColour
, true);
504 const Colour
getColour() const
506 return getImageColour (owner
, role
);
509 void resetToDefault() {}
511 void changeListenerCallback (ChangeBroadcaster
*)
517 ImageButton
* const owner
;
518 ComponentLayout
& layout
;
519 const ImageRole role
;
522 //==============================================================================
523 static void updateButtonImages (JucerDocument
& document
, ImageButton
* const ib
)
525 Image norm
= document
.getResources().getImageFromCache (getImageResource (ib
, normalImage
));
526 Image over
= document
.getResources().getImageFromCache (getImageResource (ib
, overImage
));
527 Image down
= document
.getResources().getImageFromCache (getImageResource (ib
, downImage
));
529 ib
->setImages (false, true, doesImageKeepProportions (ib
),
531 getImageOpacity (ib
, normalImage
),
532 getImageColour (ib
, normalImage
),
534 getImageOpacity (ib
, overImage
),
535 getImageColour (ib
, overImage
),
537 getImageOpacity (ib
, downImage
),
538 getImageColour (ib
, downImage
));
543 #endif // __JUCER_IMAGEBUTTONHANDLER_JUCEHEADER__